<?php

class GuestbookController extends Zend_Controller_Action {

    /**
     * Проверяем, что страница и права для нее определены
     */
    public function preDispatch() {
        if (!defined('PAGE_ID'))
            $this->_redirect(SITE_URL);
    }

    /**
     * Отображение секции
     */
    public function indexAction() {
        $forms = new GuestbookForm();
        $form = $forms->add();
        $model = new GuestbookModel();

        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $page = (int) $this->getRequest()->getParam('page', 1);

        $this->view->form = $form;
        $this->view->saccesssend = false;

        if ($this->_request->isPost()) {//Форма отправлена
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $model->newMessage(array(
                    'sid' => $sid,
                    'name' => $formData['name'],
                    'text' => $formData['text'])
                );
                $this->view->saccesssend = true;
            }
        }
        //Исправить на TRUE, если не требуется использовать проверку
        $this->view->messages = $model->getPaginatorRows($page, $sid, false);
        $this->view->paginator = $this->view->messages->getPages();
    }

    /**
     * Редактирование секции
     */
    public function modifyAction() {
        $action = $this->getRequest()->getParam('a', '');
        switch ($action) {
            case 'edit':
                $this->_forward('edit');
                break;
            case 'delete':
                $this->_forward('delete');
                break;
            default:
                $model = new GuestbookModel();
                $sid = (int) $this->getRequest()->getParam('id', 0);
                $messages = $model->getMessages($sid);
                $this->view->messages = $messages;
        }
    }

    public function editAction() {
        $model = new GuestbookModel();
        $forms = new GuestbookForm();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $mid = (int) $this->getRequest()->getParam('mid', 0);
        $form = $forms->edit($sid, $mid);
        $message = $model->getMessage($mid);
        $this->view->message = $message;
        $this->view->form = $form;
        if ($this->_request->isPost()) {//Форма отправлена
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $model->updateMessage(array(
                    'sid' => $sid,
                    'name' => $formData['name'],
                    'time' => $formData['time'],
                    'text' => $formData['text'],
                    'active' => $formData['active']
                        ), $mid);
                $this->_redirect('/pages/modify/id/' . $sid);
            }
        } else {
            $form->populate(array(
                'name' => $message->name,
                'time' => $message->time,
                'text' => $message->text,
                'active' => $message->active,
            ));
        }
        $this->view->navigation_line = $this->view->navigation_line + array($this->view->t->_('Edit message') => ADMIN_URL . '/pages/modify/id/' . $sid . '/a/edit/mid/' . $mid);
    }

    /**
     * Удаление сообщения
     */
    public function deleteAction() {
        $model = new GuestbookModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $mid = (int) $this->getRequest()->getParam('mid', 0);
        $model->deleteMessage($mid);
        $this->_redirect('/pages/modify/id/' . $sid);
    }

    /**
     * Добавление секции
     */
    public function addAction() {
        
    }

}
